<template>
  <div v-loading="loading">
    <div v-show="kind==='list'" class="transition-box">
      <el-button @click="add" type="primary" size="small">添加</el-button>
      <el-button type="primary" size="small">批量导入</el-button>
      <driver-list @edit="edit" @del="del"></driver-list>
    </div>
    <div v-show="kind==='edit'">
      <driver-edit :driver="currentDriver" @submitEdit="submitEdit" @cancelEdit="cancelEdit"></driver-edit>
    </div>
  </div>
</template>

<script>
  import DriverList from './subs/DriverList.vue';
  import DriverEdit from './subs/DriverEdit.vue';
  import * as actionTypes from '../../constants/action-types';
    export default {
        name: 'Driver',
        components:{
          DriverList,
          DriverEdit,
        },
        data() {
            return {
                loading: false,
                kind: 'list', // list, edit
              currentDriver: null,
            }
        },
      created(){
        this.fetchData();
      },
        methods:{
          fetchData(){
            let me =this;
            me.loading=true;
            this.$store.dispatch(actionTypes.GET_DRIVERLIST).then(()=>{
              me.loading=false
            }).catch(() => {
              me.loading=false
            })
          },
          add(){
            this.kind='edit';
            this.currentDriver=null;
          },
          edit(row){
            this.kind='edit';
            this.currentDriver=row;
          },
          del(row){
            let me = this;
            this.$confirm('确定要删除吗?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              me.loading=true;
              me.$store.dispatch(actionTypes.DELETE_DRIVER, row.id).then(()=>{
                me.loading=false
              }).catch(() => {
                me.loading=false;
              })
            }).catch(() => {
            });
          },
          submitEdit(newDriver){
            let me = this;
            me.loading=true;
            this.$store.dispatch(newDriver.id?actionTypes.EDIT_DRIVER:actionTypes.ADD_DRIVER, newDriver).then(()=>{
              me.kind='list';
              me.loading=false
            }).catch(() => {
              me.loading=false;
            })
          },
          cancelEdit(){
            this.kind='list';
          }
        }
    }
</script>

<style lang="stylus" scoped>
</style>
